var page = 'historyDataList'

$(document).ready(function () {
    GetRequestData(GetRequest())
});

function GetRequestData(list){
    if(JSON.stringify(list) != "{}"){
        findId(list['id'])
    }else{
        getData(p,getValue(page))
    }
}

function getData(p,arr){
    // console.log(arr);
    $.ajax({
        url: global.apiUrl + '/get_user_fatdata',
        type: 'GET',
        dataType: 'json',
        data: {
            p: p,
            start_time: arr[0],
            end_time: arr[1],
            type: arr[2],
            nickname: arr[3],
            user_id: arr[4],
            sex: arr[5]
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            if(arr[6] == 1){
                //正序
                res.content = res.content.reverse()
            }
            var html = ''
            for(var i in res.content){
                html += `<tr>
                            <td>
                                <div class="userItem" onclick="findId(${res.content[i].user_id})">
                                    <text>${res.content[i].nickname}</text>
                                    <text>${res.content[i].sex} ${res.content[i].height}cm</text>
                                    <text style="margin-bottom:0">ID: ${res.content[i].user_id}</text>
                                </div>
                            </td>
                            <td>${res.content[i].age}</td>
                            <td>${res.content[i].weight}</td>
                            <td>${res.content[i].bmi}</td>
                            <td>${res.content[i].bmr}</td>
                            <td>${res.content[i].skeletoncontent}</td>
                            <td>${res.content[i].musclemass}</td>
                            <td>${res.content[i].visceralfatrate}</td>
                            <td>${res.content[i].watercontent}</td>
                            <td>${res.content[i].bodyfat}</td>
                            <td>${res.content[i].protein}</td>
                            <td>${res.content[i].bodyage}</td>
                            <td>${res.content[i].createtime}</td>
                        </tr>`
            }
            $('#userList').html(html)
            $("#page").paging({
                pageNo: p,
                totalPage: Math.ceil(res.count/20),
                totalSize: res.count,
                callback: function(num) {
                    // console.log(num)
                    getData(num,getValue(page))
                }
            })
        }
    })
}

//搜索
function search(obj)
{
    var searchInput = document.getElementById("searchInput");
    var searchSelect = document.getElementById("searchSelect");
    if(obj.value == 'name' || obj.value == 'ID'){
        searchInput.style.display = 'block';
        searchSelect.style.display = 'none'
    }else{
        searchInput.style.display = 'none';
        searchSelect.style.display = 'block'
        getData(p,getValue(page))
    }
}

function sort(obj){
    getData(p,getValue(page))
}

function clickBtn(){
    getData(p,getValue(page))
}

function findId(id){
    // console.log(id)
    $("#searchSelectMain").val("ID");
    $("#searchInput").val(id);
    getData(p,getValue(page))
}
